<template>
    <div>
    <el-form inline class="demo-form-inline">
    <el-row >
    <el-col :span="6">
        <el-form-item label="标题">
            <el-input v-model="query.payload.title" placeholder="标题" />
        </el-form-item>
    </el-col>
    </el-row>
    <el-row >
        <el-form-item label="时间范围">
            <el-date-picker
                    v-model="query.dateRange"
                    type="datetimerange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="Search" activity @click="queryArticle">查询</el-button>
            <el-button type="warning" icon="Refresh" activity @click="reset">重置</el-button>
            <el-button type="primary" icon="Plus" @click="addArticle">新增</el-button>
        </el-form-item>
    </el-row>
    </el-form>
    <el-divider style="margin: 12px 0"/>
    <el-table ref="table" border v-loading="loading"  :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index"  label="序号" :width="80" />
        <el-table-column align="center" prop="title" label="标题" />
        <el-table-column align="center" prop="coverImg" label="封面图" >
            <template #default="scope">
                <el-image
                        style="width: 100px; height: 100px"
                        :preview-src-list="[scope.row.coverImg]"
                        :src="scope.row.coverImg" fit="fill" :preview-teleported="true" />
            </template>

        </el-table-column>
        <el-table-column align="center"  prop="updateTime" label="更新时间" >
            <template #default="scope">
                {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
            </template>
        </el-table-column>
        <el-table-column align="center"  label="操作">
            <template #default="scope">
                <el-link title="查看" type="primary" @click="readArticle(scope.row.id)">
                    查看
                </el-link>
                &nbsp;
                <el-link title="修改" type="primary" @click="editArticle(scope.row.id)">
                    修改
                </el-link>
                &nbsp;
                <el-popconfirm title="删除？" @confirm="deleteArticle(scope.row.id)"  @cancel="">
                    <template #reference>
                        <el-link type="danger" title="删除">
                            修改
                        </el-link>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
</el-table>
<el-pagination
        style="display: flex;align-items:center;justify-content:center;margin-top:10px"
        v-model:currentPage="query.currentPage"
        v-model:page-size="query.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.total"
        @size-change="queryArticle"
        @current-change="queryArticle"
/>
<ModArticle @close="data.modArticle = false"  @reload="queryArticle" :modArticle="data.modArticle" :type="data.type" :id="data.id" />
</div>
</template>

<script setup>
    import ModArticle from '@/views/system/components/system/ModArticle.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'

    let data = reactive({
        modRole:false,
        tableData:[],
        type:"",
        id:"",
    });
    let loading = ref(true)
    let query = reactive({dateRange:[],payload:{}, currentPage:1,pageSize:10,total:0})
    onMounted(() => {
        queryArticle()
    })
    function queryArticle(){
        query.startTime = query.dateRange[0]
        query.endTime = query.dateRange[1]
        request.post("/api/article/getArticlePage", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function reset(){
            query.payload.title = ""
            query.payload.content = ""
            query.payload.coverImg = ""
    }

    function addArticle(){
        data.modArticle = true
        data.type = "add"
    }

    function deleteArticle(id){
        request.get(`/api/article/delete/${id}`).then(resp => {
            queryArticle()
            ElMessage({message: '删除成功！',type: 'success'})
        });
    }

    function editArticle(id){
        data.id = id
        data.type = "edit"
        data.modArticle = true
    }
    function readArticle(id){
        data.id = id
        data.type = "read"
        data.modArticle = true
    }

</script>
<style lang="css">

</style>


